export const instance = async (url, data = {}, type = "POST") => {
	// const baseUrl = `http://zhangyu.aizhangyu.cn/${url}`;
	// const baseUrl = `http://test.shzx100.com/${url}`;
	const baseUrl = `http://43.137.6.25/${url}`;
	let tokenInfo = uni.getStorageSync("USER_TOKEN_INFOS");
	if (tokenInfo && tokenInfo.token) {
		data.token = tokenInfo.token;
	}
	if (!data.language) {
		data.language = uni.getStorageSync('bofangmoshi') + '' === '1' ? 'us' : 'en'
	}
	console.log(baseUrl);
	console.log(data);
	try {
		return new Promise((resolve, reject) => {
			uni.request({
				url: baseUrl,
				data: data,
				method: type,
				dataType: "json",
				sslVerify: true,
				header: {
					token: data.token || "",
				},
				success(res) {
					if (res.data.code == 401) {
						uni.removeStorageSync("USER_TOKEN_INFOS");
						uni.showToast({
							title: '请重新登录吧',
							icon: 'none',
							duration: 1300
						})
						setTimeout(() => {
							uni.navigateTo({
								url: "/pages/login/login",
							});
						}, 200);
					}
					resolve(res.data);
				},
				fail(err) {
					if (
						err.errMsg.includes(
							"request:fail abort statusCode:-1 Failed to connect to"
						)
					) {
						uni.showToast({
							title: "网络出现故障",
							icon: "none",
							duration: 2200
						});
					}
					reject(err);
				},
			});
		});
	} catch (err) {
		uni.showToast({
			title: err.msg || "请求失败",
			icon: "error",
		});
		return err;
	}
};